package com.changxianggu.cloud.edu.tool.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.edu.tool.constant.CommonConstant;
import com.changxianggu.cloud.edu.tool.constant.HttpConstant;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author liuyuantao
 */
@Slf4j
public class MainController extends BaseController {
    @FXML
    public Label currentVersion;
    @FXML
    public Hyperlink latestVersion;
    @FXML
    public Button genNewVersionBtn;

    public void doHandlerExcelView() {
        URL url = Thread.currentThread().getContextClassLoader().getResource("fxml/HandleExcel.fxml");
        openWindow(url, 600, 570, CommonConstant.MAIN_EXCEL_TITLE);
    }

    public void doHandlerDbView() {
        URL url = Thread.currentThread().getContextClassLoader().getResource("fxml/ConnectDb.fxml");
        openWindow(url, 500, 400, CommonConstant.MAIN_DB_TITLE);
    }

    private void openWindow(URL url, Integer width, Integer height, final String title) {
        log.debug("准备打开{}", title);
        try {
            Parent platform = FXMLLoader.load(url);
            Stage platformStage = new Stage();
            platformStage.setTitle(title);
            platformStage.setResizable(false);
            platformStage.getIcons().add(new Image("image/icon.png"));
            platformStage.setScene(new Scene(platform, width, height));
            platformStage.show();
            log.debug("{} 打开成功", title);
            platformStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent event) {
                    log.debug("{}关闭", title);

                }
            });
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        String currentVersionStr = "当前版本:V" + CommonConstant.CURRENT_VERSION;
        currentVersion.setText(currentVersionStr);
        HttpResponse execute = HttpRequest.get(HttpConstant.EDU_TOOL_VERSION_URL).execute();
        JSONObject jsonData = JSONObject.parseObject(execute.body());
        JSONObject jsonObject = jsonData.getJSONObject("data");
        if (jsonObject != null) {
            Object versionNum = jsonObject.get("versionNum");
            if (versionNum != null) {
                String latestVersionStr = versionNum.toString();
                latestVersion.setText("V" + latestVersionStr);
                if (!StrUtil.equalsIgnoreCase(latestVersionStr, CommonConstant.CURRENT_VERSION)) {
                    genNewVersionBtn.setVisible(true);
                }
            }
        } else {
            latestVersion.setText("V" + CommonConstant.CURRENT_VERSION);
        }
        log.debug("主界面加载成功");
    }
}
